/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : WordData.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 21, 2008       Raghu Sarraff			       Created
 */
package lwf.factory;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

import net.lwf.handler.util.WordUtil;

import lwf.handler.entities.english.word.Word;


/**
 * Type the class description here
 *  
 */
public class WordData {
	
	private static WordData instance = null;
	private ResourceBundle wordsBundle = null;
	
	//word types
	private List verySimpleWords = new ArrayList();
	private List simpleWords = new ArrayList();
	private List meduimWords = new ArrayList();
	private List aboveMeduimWords = new ArrayList();
	private List hardWords = new ArrayList();
	
	private WordData() {
		init();
	}

	public synchronized final static WordData getInstance(){
		if (instance == null) {
			instance = new WordData();
		}
		return instance;
	}
	
	//-----------------------------------------------------------------------------------
	// Exposed methods
	//-----------------------------------------------------------------------------------
	/**
	 * @return Returns the aboveMeduimWords.
	 */
	public List getAboveMeduimWords() {
		return aboveMeduimWords;
	}
	/**
	 * @return Returns the hardWords.
	 */
	public List getHardWords() {
		return hardWords;
	}
	/**
	 * @return Returns the meduimWords.
	 */
	public List getMeduimWords() {
		return meduimWords;
	}
	/**
	 * @return Returns the simpleWords.
	 */
	public List getSimpleWords() {
		return simpleWords;
	}
	/**
	 * @return Returns the verySimpleWords.
	 */
	public List getVerySimpleWords() {
		return verySimpleWords;
	}	
	
	//-----------------------------------------------------------------------------------
	// Private methods
	//-----------------------------------------------------------------------------------
	
	private void init() {
		if(wordsBundle == null) {
			wordsBundle = ResourceBundle.getBundle("resources.words");
			this.processWords(wordsBundle);
		}
	}
	
	private void processWords(ResourceBundle resource) {
		
		Enumeration enum1 = resource.getKeys();
		while(enum1.hasMoreElements()) {
			String key = enum1.nextElement().toString();
			String value = resource.getString(key);
			StringTokenizer stk = new StringTokenizer(value, "|");
			while(stk.hasMoreTokens()) {
				String word = stk.nextToken().trim();
				String wordtype = stk.nextToken().trim();
				String sentences = stk.nextToken().trim();
				Word wordinfo = new Word(word, wordtype, sentences);
				if(WordUtil.isVerySimple(word)) {
					verySimpleWords.add(wordinfo);
				}
				else if(WordUtil.isSimple(word)) {
					simpleWords.add(wordinfo);
				}
				else if(WordUtil.isMediumWord(word)) {
					meduimWords.add(wordinfo);
				}
				else if(WordUtil.isAboveMediumWord(word)) {
					aboveMeduimWords.add(wordinfo);
				}
				else {
					hardWords.add(wordinfo);
				}
			}
		}		
	}

}

